package com.bt.controller;



import com.bt.entity.User;
import com.bt.service.UserService;
import com.github.pagehelper.PageInfo;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    private UserService uService;
    //去登录页面
	/*	@GetMapping("/toLogin")
	public String toLogin(){
		return "index";
	}*/
    //登录
    @PostMapping("/dologin")
    public String login(
            @RequestParam(value="name") String name,
            @RequestParam(value="password") String password
            , Map<String,Object> maps, HttpSession session) {
        User user = uService.login(name, password);
        if (user != null) { //将用户信息保存到session中去用户列表页面
            session.setAttribute("user", user);
            return "redirect:/main";//重定向到/main请求-防止表单重复提交
        } else { //去登录页面
            maps.put("msg", "用户名或密码不正确!");
            return "login";
        }
    }
//    //查询所有用户
//    @GetMapping("/getUserList")
//   public String getUserList(Model model){
//        List<User> userList = uService.getUserList();
//        if(userList!=null){
//            model.addAttribute("userList",userList);
//            //return "main";
//            return "ulist";
//        }else {
//            return null;
//        }
//    }
    //查看详情
    @GetMapping("/user/{id}")
    public String selectById(@PathVariable("id") Integer id, Model model){
        User user=uService.selectUserById(id);
        model.addAttribute("user",user);
        return "detail";
    }
    //删除用户
    @RequestMapping("/delUser/{id}")
    public String delUser(@PathVariable("id") Integer id){
        uService.delUser(id);
        return "redirect:/getUserList";
    }
    @GetMapping("/toUpdateUser/{id}")
    public String toUpdate(@PathVariable("id") Integer id,Model model){
        User user = uService.selectUserById(id);
        model.addAttribute("user",user);
        return "update";
    }
    @RequestMapping("/updateUser")
    public String updateUser(User user){
        uService.updateUser(user);
        return "redirect:/getUserList";
    }
    @RequestMapping ("/addUser")
    public String addUser(User user){
        uService.addUser(user);
        return "redirect:/getUserList";
    }
    @GetMapping("/addUser")
    public String toadd(){
        return "add";
    }
    // 处理注册请求
    @PostMapping("/register")
    public String register(User user,Model model) {
        // 1. 检查用户名是否已存在
        if (uService.findByUsername(user.getName()) != null) {
            model.addAttribute("msg", "用户名已被占用");
            return "register";
        }
        uService.saveUser(user);
        // 2. 注册成功，跳转到登录页
        return "login";
    }

    @GetMapping("/quit")
    public String toquit(){
        return "login";
    }
    //改造原来的查询方法
    //改造原来的查询方法
    @GetMapping("/getUserList")
    public String list(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "sex", required = false) String sex,
            @RequestParam(value = "address", required = false) String address,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            Model model) {
        // 调用分页查询
        PageInfo<User> pageInfo = uService.findByCondition(name, sex, address, pageNum, pageSize);
        // 将结果传递给前端
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("name",name);
        model.addAttribute("sex",sex);
        model.addAttribute("address",address);
        return "ulist";
    }

}
